import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import Layout from '@/Layout.vue'

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'Layout',
      component: Layout,
      alias: '/layout',
      children: [
        {
          //流程
          path: 'flowpath',
          name: 'flowpath',
          component: () => import('../views/FlowPath.vue')
        },
        {
          //库存
          path: 'warehouse',
          name: 'warehouse',
          component: () => import('../views/warehouse.vue')
        },
        {
          // 仓库信息
          path: 'warehousemessage',
          name: 'warehousemessage',
          component: () => import('../views/WarehouseMessage.vue')
        },
        {
          //出货
          path: 'shipment',
          name: 'shipment',
          component: () => import('../views/shipmentView.vue')
        },
        {
          //存储
          path: 'usercenter',
          name: 'usercenter',
          component: () => import('../views/UserCenter.vue')
        },
        {
          //存储
          path: 'User_Add',
          name: 'User_Add',
          component: () => import('../views/User_Add.vue')
        }
      ]
    },
    {
      //登录
      path: '/loginview',
      name: 'loginview',
      component: () => import('../views/LoginView.vue')
    },
    {
      //404
      path: '/:catchAll(.*)',
      component: () => import('../views/unknown.vue')
    }
  ]
  
});
// 路由守卫
router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  const name = localStorage.getItem('TOKEN'); // 假设你使用 localStorage 存储认证状态

  if (requiresAuth && !name) {
    next('/loginview'); // 如果需要认证且未认证，重定向到登录页面
  } else {
    next(); // 否则继续导航
  }
});

export default router
